//
//  YHY_Setting_VC.m
//  Travels
//
//  Created by yanghuiyan on 16/3/19.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "YHY_Setting_VC.h"
#import "SettingTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "YHYBaseTableView.h"
#import "YHYBaseView.h"
#import "DKNightVersionManager.h"
#define Width self.view.bounds.size.width
#define Height self.view.bounds.size.height
@implementation YHY_Setting_VC

- (void)dealloc {
    
    _tabelView.delegate = nil;
    _tabelView.dataSource = nil;
    
    [_tabelView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createView];

    
}

#pragma mark - createView
- (void) createView {
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"我的设置";
    titleLabel.font = [UIFont boldSystemFontOfSize:25];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];

    
    //设置左侧返回按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    [leftItem release];
    
    self.tabelView = [[YHYBaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tabelView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"SettingTableViewCellIdentifier"];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [self.view addSubview:self.tabelView];
    
    [_tabelView release];
    
}


#pragma mark - UITableViewDataSource协议方法实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     SettingTableViewCell *cell = [self.tabelView dequeueReusableCellWithIdentifier:@"SettingTableViewCellIdentifier"];
    if (indexPath.section == 0) {
        
        // 设置label显示缓存大小
        cell.titleLabel.frame = CGRectMake(10*Width/416, 10*Height/736, 300 *Width/416, 30*Height/736);
        // 计算需要清理的缓存
        NSInteger size = [[SDImageCache sharedImageCache]getSize];
        float sizeMB = size/1024.0/1024;
        cell.titleLabel.text = [NSString stringWithFormat:@"需要清理的缓存            %.2fM", sizeMB];
        [cell.contentView addSubview:cell.titleLabel];
        
        return cell;
    }else {
        
        [self removeAllSubviewOnView:cell.contentView];

        SettingTableViewCell *cell = [self.tabelView dequeueReusableCellWithIdentifier:@"SettingTableViewCellIdentifier"];
        
        cell.label.text = @"开启夜间模式";
        //添加button按钮
        cell.sw.frame = CGRectMake(350*Width/416, 15*Height/736, 40*Width/416, 20*Height/736);
        
        [cell.sw addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventValueChanged];
        
        NSString *night = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
        //判断switch的状态 如果状态为YES 就让开关为开着的状态
        if ([night isEqualToString:@"YES"]) {
            cell.sw.on = YES;
        }else{
            //如果是NO 就让开关为关着的状态
            cell.sw.on = NO;
        }

        
        [cell.contentView addSubview:cell.label];
        [cell.contentView addSubview:cell.sw];
        
        [cell.label release];
        [cell.sw release];
        return cell;

    }
}

// 移除子视图
- (void)removeAllSubviewOnView:(UIView *)view{
    NSArray *array = view.subviews;
    for (UIView *v in array) {
        [v removeFromSuperview];
    }
}
#pragma mark - swith点击方法
// swith点击变为夜间模式
- (void)buttonAction:(UIButton *)sender {
    
    NSString *state = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([state isEqualToString:@"YES"]) {
        //变成非夜间模式状态
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"night"];
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor whiteColor]];
        
    }else{
        //是夜间模式状态
        [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"night"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor colorWithRed:100/255.0 green:106/255.0 blue:107/255.0 alpha:1.0]];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YHYBaseView *sectionView = [[[YHYBaseView alloc] initWithFrame:CGRectMake(0, 0, Width, 30*Height/736)] autorelease];
    [sectionView setBackgroundColor:[UIColor colorWithRed:248 / 255.0 green:249 / 255.0 blue:240 / 255.0 alpha:1]];
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存？" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //清除缓存
//            NSInteger size = [[SDImageCache sharedImageCache]getSize];
//            float sizeMB = size/1024.0/1024;
            [[SDImageCache sharedImageCache]clearDisk];
            //            NSLog(@"sizeMB = %f",sizeMB);
            [self.tabelView reloadData];
            
        }];
        [alertC addAction:action];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:cancelAction];
    }

}

#pragma mark - 左侧搜索按钮
- (void)leftAction:(UIBarButtonItem *) sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
